﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mbs.Threading;
using Mbs.Net.Tcp;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.IO;
using System.Net;
using System.Runtime.Serialization;

namespace Mbs.Net
{
   
    public interface IServiceHandler
    {
        HostInfo Info { get; }
        IServiceHost Host { get; }

        void Init(HostInfo info, IServiceHost host, Socket socket);
    }

   

    public abstract class ServiceHandler : IServiceHandler
    {
       
        protected Socket Socket { get;private set;}

        public HostInfo Info { get; set; }
        public IServiceHost Host { get; private set; }


        private EventHandler<EventArgs<Exception>> _exceptionFired;
        public event EventHandler<EventArgs<Exception>> ExceptionFired
        {
            add { _exceptionFired += value; }
            remove { _exceptionFired -= value; }
        }

        protected internal virtual void OnExceptionFired(Exception ex)
        {
            if (_exceptionFired != null)
                _exceptionFired(this, new EventArgs<Exception>(ex));
        }

        public void Init(HostInfo info, IServiceHost host, Socket socket)
        {
            Host = host;
            Socket = socket;
            Info = info;

            OnInit();
        }

        protected virtual void OnInit() { }
    }

   
    
}
